// pages/cart/cart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cartslist: [],
        count: 1,
        total: 0,
        istype: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

        this.setData({
            cartslist: wx.getStorageSync('carts')
        })


    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //改变选中状态
    changeChecked(e) {
        // console.log(e.currentTarget.dataset.index)
        //拿到当前商品对应的索引
        let index = e.currentTarget.dataset.index
        //拿到购物车数据
        let carts = this.data.cartslist
        //修改该商品中的selected属性
        carts[index].selected = !carts[index].selected
        //修改购物车
        this.setData({
            cartslist: carts
        })
        this.updateCartstsData()
        this.countprice()
        this.dhjs()
    },
    remove(e) {
        let index = e.currentTarget.dataset.index
        //拿到购物车数据
        let carts = this.data.cartslist
        let that = this
        //修改该商品中的selected属性
        wx.showModal({
            title: "商品",
            content: "不要了嘛！",
            success(e) {
                if (e.confirm) {
                    carts.splice(index, 1)
                    that.setData({
                        cartslist: carts
                    })
                    //修改购物车
                    that.updateCartstsData()
                }
            }
        })
    },
    updateCartstsData() {
        let carts = this.data.cartslist
        wx.setStorageSync('carts', carts)
    },
    getCount(e) {
        let index = e.currentTarget.dataset.index
        //拿到购物车数据
        let carts = this.data.cartslist
        let that = this
        carts[index].number = e.detail.count
        this.setData({
            count: e.detail.count
        })
        that.updateCartstsData()
        that.countprice()
        that.dhjs()
    },
    countprice() {
        let carts = this.data.cartslist
        let aa = 0
        carts.forEach(res => {
            console.log(res)
            if (res.selected) {
                aa += res.number * res.price

            }

        })
        this.setData({
            total: aa
        })
    },
    awmbn(e) {
        let carts = this.data.cartslist
        let istype = this.data.istype

        carts.forEach(res => {
            res.selected = true

        })
        if(istype){
           carts.forEach(res=>{
               res.selected = false
           })
        }

        this.setData({
            istype: !istype,
            cartslist: carts
        })
        this.countprice()
    },
    dhjs() {
        let carts = this.data.cartslist
        let istype = false;
        carts.forEach(res => {
            if (res.selected) {
                istype = true
            }
        })
        if (!istype) {
            this.setData({
                istype: istype
            })
        }
    }

})